local fumian = fk.CreateSkill {

  name = "joy__fumian",

  tags = {  },

}



fumian:addEffect(fk.EventPhaseStart, {
  name = "joy__fumian",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fumian.name) and player.phase == Player.Start
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("joy__fumian1_record") > 0 or player:getMark("joy__fumian2_record") > 0 then
      for i = 1, 2, 1 do
        if player:getMark("joy__fumian"..i.."_record") > 0 then
          room:setPlayerMark(player, "joy__fumian"..i.."_record", 0)
          room:setPlayerMark(player, "joy__fumian"..i.."-tmp", 1)
        end
      end
    end
    self:doCost(event, target, player, data)
    room:setPlayerMark(player, "joy__fumian1-tmp", 0)
    room:setPlayerMark(player, "joy__fumian2-tmp", 0)
  end,
  on_cost = function(self, event, target, player, data)
    local choices = {"joy__fumian1:::"..(player:getMark("joy__fumian2-tmp") + 1), "joy__fumian2:::"..(player:getMark("joy__fumian1-tmp") + 1), "Cancel"}
    local choice = player.room:askForChoice(player, choices, fumian.name)
    if choice ~= "Cancel" then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = event:getCostData(self)[7]
    if n == "1" then
      room:setPlayerMark(player, "@joy__fumian1-turn", player:getMark("joy__fumian2-tmp") + 1)
    else
      room:setPlayerMark(player, "@joy__fumian2-turn", player:getMark("joy__fumian1-tmp") + 1)
    end
    room:setPlayerMark(player, "joy__fumian"..n.."_record", 1)
  end,
})

fumian:addEffect(fk.DrawNCards, {
  name = "#joy__fumian_trigger",
  mute = true,
  events = {fk.DrawNCards, fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    if target == player then
      if event == fk.DrawNCards then
        return player:getMark("@joy__fumian1-turn") > 0
      else
        return player:getMark("@joy__fumian2-turn") > 0 and data.card.color == Card.Red and data.tos and
          #data:getUseExtraTargets() > 0
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.DrawNCards then
      return true
    else
      local tos = player.room:askForChoosePlayers(player, data:getUseExtraTargets(), 1, player:getMark("@joy__fumian2-turn"),
        "#fumian-choose:::"..data.card:toLogString()..":"..player:getMark("@joy__fumian2-turn"), "joy__fumian", true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      data.n = data.n + player:getMark("@joy__fumian1-turn")
      room:setPlayerMark(player, "@joy__fumian1-turn", 0)
    else
      for _, id in ipairs(event:getCostData(self)) do
        TargetGroup:pushTargets(data.tos, id)
      end
    end
  end,
})
fumian:addEffect(fk.AfterCardTargetDeclared, {
  name = "#joy__fumian_trigger",
  mute = true,
  events = {fk.DrawNCards, fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    if target == player then
      if event == fk.DrawNCards then
        return player:getMark("@joy__fumian1-turn") > 0
      else
        return player:getMark("@joy__fumian2-turn") > 0 and data.card.color == Card.Red and data.tos and
          #data:getUseExtraTargets() > 0
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.DrawNCards then
      return true
    else
      local tos = player.room:askForChoosePlayers(player, data:getUseExtraTargets(), 1, player:getMark("@joy__fumian2-turn"),
        "#fumian-choose:::"..data.card:toLogString()..":"..player:getMark("@joy__fumian2-turn"), "joy__fumian", true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      data.n = data.n + player:getMark("@joy__fumian1-turn")
      room:setPlayerMark(player, "@joy__fumian1-turn", 0)
    else
      for _, id in ipairs(event:getCostData(self)) do
        TargetGroup:pushTargets(data.tos, id)
      end
    end
  end,
})

return fumian